import { NextResponse } from 'next/server'
import { fetchWpPostById } from './service'

// GET 请求处理
export async function GET(req: Request) {
  try {
    const { searchParams } = new URL(req.url)
    const id = searchParams.get('id')

    if (!id) {
      return NextResponse.json({ error: '请输入查询id' }, { status: 400 })
    }

    const post = await fetchWpPostById(id)
    const data = post ? [post] : []

    return NextResponse.json({ data }, { status: 200 })
  } catch (error) {
    console.error('GET请求获取文章详情失败:', error)
    return NextResponse.json(
      {
        code: 500,
        message: '服务器内部错误',
        error: error instanceof Error ? error.message : 'Unknown error',
      },
      { status: 500 }
    )
  }
}
